<?php


namespace App\Http\Server;


use App\Models\Manufacturer;
use App\Models\Restock;

class IndexServer
{

    public function getRestockInfoServer($params){
        //统计一定时期内的进货总量、总金额、各厂家进货占比等数据。
        $selectedTab = $params['selectedTab'];
        if($selectedTab == '日报'){
            $startDate = date('Y-m-d 00:00:00');
            $endDate = date('Y-m-d 23:59:59');
        }elseif ($selectedTab == '周报'){
            // 获取本周周一日期
            $startDate = date('Y-m-d 00:00:00', strtotime('last monday'));
            // 获取本周周日日期
            $endDate = date('Y-m-d 23:59:59', strtotime('last sunday'));
        }elseif ($selectedTab == '月报'){
            // 获取本周周一日期
            $startDate = date('Y-m-01 00:00:00');
            // 获取本周周日日期
            $endDate = date('Y-m-t 23:59:59');
        }
        // 获取请求中的开始时间和结束时间
        // 查询一定时期内的进货记录
        $query = Manufacturer::withSum([
            'restock' => function ($query) use ($startDate, $endDate) {
                if ($startDate) {
                    $query->where('now_date', '>=', $startDate);
                }
                if ($endDate) {
                    $query->where('now_date', '<=', $endDate);
                }
            }
        ], 'num')->withSum([
            'restock' => function ($query) use ($startDate, $endDate) {
                if ($startDate) {
                    $query->where('now_date', '>=', $startDate);
                }
                if ($endDate) {
                    $query->where('now_date', '<=', $endDate);
                }
            }
        ], 'total_price');

        $suppliers = $query->get();

        // 计算总进货金额和总量
        $totalPrice = $suppliers->sum('restock_sum_total_price');
        $totalNum = $suppliers->sum('restock_sum_num');

        $result = [];
        foreach ($suppliers as $restock) {
            if ($totalPrice > 0) {
                $totalPricePercentage = ($restock->restock_sum_total_price / $totalPrice) * 100;
            } else {
                $totalPricePercentage = 0;
            }

            if ($totalNum > 0) {
                $totalNumPercentage = ($restock->restock_sum_num / $totalNum) * 100;
            } else {
                $totalNumPercentage = 0;
            }

            $result[] = [
                'manufacturer_name' => $restock->manufacturer_name,
                'restock_sum_total_price' => $restock->restock_sum_total_price,
                'total_price_percentage' => $totalPricePercentage,
                'restock_sum_num' => $restock->restock_sum_num,
                'total_num_percentage' => $totalNumPercentage
            ];
        }

        $data = [
            'total_price' => $totalPrice,
            'total_quantity' => $totalNum,
            'manufacturer_statistics' => $result
        ];
        return ['status'=>200,'data'=>$data,'msg'=>'success'];
    }

    //首页数据面板：销售金额，订单数量，固定用户，流量用户，进货数，成本金额
    public function getIndexDataServer($params){
        $selectedTab = $params['selectedTab'];
        if($selectedTab == '日报'){
            $startDate = date('Y-m-d 00:00:00');
            $endDate = date('Y-m-d 23:59:59');
        }elseif ($selectedTab == '周报'){
            // 获取本周周一日期
            $startDate = date('Y-m-d 00:00:00', strtotime('last monday'));
            // 获取本周周日日期
            $endDate = date('Y-m-d 23:59:59', strtotime('last sunday'));
        }elseif ($selectedTab == '月报'){
            // 获取本周周一日期
            $startDate = date('Y-m-01 00:00:00');
            // 获取本周周日日期
            $endDate = date('Y-m-t 23:59:59');
        }
    }

    //获取销售金额
    public function getSalesAmount($startDate,$endDate){

    }


}
